package com.tianyi.lib.model;

import java.io.Serializable;

/**
 * web result
 *
 * @author cuixuhui
 */
public class WebResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 响应业务状态
     */
    private Integer status;

    private boolean success;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应中的数据
     */
    private T data;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> WebResult<T> ok(T data){
        return WebResult.ok(200, data);
    }

    public static <T> WebResult<T> ok(Integer status, T data){
        return ok(status,"success", data);
    }

    public static <T> WebResult<T> ok(Integer status, String msg, T data){
        return getInstance(status, true, msg, data);
    }

    public static <T> WebResult<T> error(String msg){
        return WebResult.error(msg, null);
    }

    public static <T> WebResult<T> error(String msg, T data){
        return error(500,  msg, data);
    }

    public static <T> WebResult<T> error(Integer status, String msg, T data){
        return getInstance(status, false, msg, data);
    }

    private static <T> WebResult<T> getInstance(Integer status, boolean success, String msg, T data){
        WebResult<T> result = new WebResult<T>();
        result.setStatus(status);
        result.setSuccess(success);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}
